<?php
class Application_Model_dataDAO_BanTinDAO implements Application_Model_dataDAOInf_BanTinDAOInf {
	/**
	 * 
	 */
	
	protected $_name = "tb_bantin";
	/**
	 * 
	 */
	
	protected static $_instance = null;
	
	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new self ();
		}
		return self::$_instance;
	}
	
	public function getAllBanTin() {
		$banTinArr = array ();
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_bantin";
			
			$result = $db->fetchAll ( $statement );
			$i = 0;
			if (count ( $result > 0 )) {
				foreach ( $result as $rs ) {
					$banTin = new Application_Model_bean_BanTin ();
					$banTin->setMaBanTin($rs['maBanTin']);
					$banTin->setTieuDe ( $rs ['tieuDe'] );
					$banTin->setTab ( $rs ['tab'] );
					$banTin->setNoiDung ( $rs ['noiDung'] );
					$banTin->setHinhKemBanTin ( $rs ['hinhKemBanTin'] );
					$banTin->setMaDanhMucBanTin ( $rs ['maDanhMucBanTin'] );
					$banTin->setMaTaiKhoan ( $rs ['maTaiKhoan'] );
					
					$banTinArr [$i] = $banTin;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $banTinArr;
	}
	
	/**
	 * @param unknown_type $idBanTin
	 */
	public function getBanTin($idBanTin) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$banTin = new Application_Model_bean_BanTin ();
		$banTin instanceof  Application_Model_bean_BanTin;
		try {
			$statement = "SELECT * FROM tb_bantin where maBanTin=" . $idBanTin;
			
			$rs = $db->fetchAll( $statement );
			foreach ($rs as $result):
			$banTin->setMaBanTin($idBanTin);
			$banTin->setTieuDe ( $result ['tieuDe'] );
			$banTin->setTab ( $result ['tab'] );
			$banTin->setNoiDung ( $result ['noiDung'] );
			$banTin->setHinhKemBanTin ( $result ['hinhKemBanTin'] );
			$banTin->setMaDanhMucBanTin ( $result ['maDanhMucBanTin'] );
			$banTin->setMaTaiKhoan ( $result ['maTaiKhoan'] );
			endforeach;
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $banTin;
	}
	
	/**
	 * 
	 */
	public function getTopBanTin() {
		$banTinArr = array ();
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_banTin LIMIT 10";
			
			$result = $db->fetchAll ( $statement );
			if (count ( $result > 0 )) {
				$i = 0;
				foreach ( $result as $rs ) {
					$banTin = new Application_Model_bean_BanTin ();
					$banTin->setTieuDe ( $rs ['tieuDe'] );
					$banTin->setTab ( $rs ['tab'] );
					$banTin->setNoiDung ( $rs ['noiDung'] );
					$banTin->setHinhKemBanTin ( $rs ['hinhKemBanTin'] );
					$banTin->setMaDanhMucBanTin ( $rs ['maDanhMucBanTin'] );
					$banTin->setMaTaiKhoan ( $rs ['maTaiKhoan'] );
					
					$banTinArr [$i] = $banTin;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $banTinArr;
	}
	
	/**
	 * @param unknown_type $maDanhMuc
	 */
	public function getBanTinTheoDanhMuc($maDanhMuc) {
		$banTinArr = array ();
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_banTin where maDanhMuc=" . $maDanhMuc;
			
			$result = $db->fetchAll ( $statement );
			if (count ( $result > 0 )) {
				$i = 0;
				foreach ( $result as $rs ) {
					$banTin = new Application_Model_bean_BanTin ();
					$banTin->setTieuDe ( $rs ['tieuDe'] );
					$banTin->setTab ( $rs ['tab'] );
					$banTin->setNoiDung ( $rs ['noiDung'] );
					$banTin->setHinhKemBanTin ( $rs ['hinhKemBanTin'] );
					$banTin->setMaDanhMucBanTin ( $rs ['maDanhMucBanTin'] );
					$banTin->setMaTaiKhoan ( $rs ['maTaiKhoan'] );
					
					$banTinArr [$i] = $banTin;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $banTinArr;
	}
	
	/**
	 * @param unknown_type $banTin
	 */
	public function addBanTin($tieuDe, $tab, $noiDung, $hinhKemBanTin, $maDanhMucBanTin, $maTaiKhoan) {
		
		$result = - 1;
			
			try {
				$db = Application_Model_dataDAO_DBConnect::conn ();
				$arr = array ("tieuDe" =>$tieuDe, "tab" => $tab, "noiDung" => $noiDung, "hinhKemBanTin" => $hinhKemBanTin, "maDanhMucBanTin" => $maDanhMucBanTin, "maTaiKhoan" => 1);
				$result = $db->insert ( 'tb_bantin', $arr );
				$result=$db->lastInsertId();
				$db->closeConnection ();
			
			} catch ( Zend_Db_Exception $e ) {
				echo $e->getMessage ();
			}
			return $result;
		
	}
	
	/**
	 * @param unknown_type $banTin
	 */
	public function editBanTin($maBanTin,$tieuDe, $tab, $noiDung, $hinhKemBanTin, $maDanhMucBanTin, $maTaiKhoan) {
		$result = false;
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
				$condition = array ("maBanTin=" . $maBanTin );
				$update = array ("tieuDe"=>$tieuDe,"tab"=>$tab,"noiDung"=>$noiDung,"hinhKemBanTin"=>$hinhKemBanTin,"maDanhMucBanTin"=>$maDanhMucBanTin,"maTaiKhoan"=>$maTaiKhoan);
				
				if ($maBanTin != NULL && $maBanTin != "")
					$update ["maBanTin"] = $maBanTin;
				
				if ($tieuDe != NULL && $tieuDe != "")
					$update ["tieuDe"] = $tieuDe;
				
				if ($tab != NULL && $tab != "")
					$update ["tab"] = $tab;
				
				if ($noiDung != NULL && $noiDung != "")
					$update ["noiDung"] = $noiDung;
				
				if ($hinhKemBanTin != NULL && $hinhKemBanTin != "")
					$update ["hinhKemBanTin"] = $hinhKemBanTin;
				
				if ($maDanhMucBanTin != NULL && $maDanhMucBanTin != "")
					$update ["maDanhMucBanTin"] = $maDanhMucBanTin;
				
				if ($maTaiKhoan != NULL && $maTaiKhoan != "")
					$update ["maTaiKhoan"] = $maTaiKhoan;
				
				$result = $db->update ( 'tb_bantin', $update, $condition );
				
				$db->closeConnection ();
		
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $result;
	}
	
	/**
	 * @param unknown_type $idBanTin
	 */
	public function deleteBanTin($idBanTin) {
		$kq = false;
		
		try {
			$dao = Application_Model_dataDAO_DBConnect::conn ();
			$dk [] = 'maBanTin=' . $idBanTin;
			
			$check = $dao->delete ( 'tb_bantin', $dk );
			
			if ($check == 1)
				$kq = true;
			
			$dao->closeConnection ();
		
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $kq;
	}

/**
 * 
 */

}

?>